package com.yh.correct.controller;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.yh.correct.model.Education;
import com.yh.correct.model.Labour;
import com.yh.correct.model.Page;
import com.yh.correct.model.ResultData;
import com.yh.correct.service.ILabourService;
/**
 * 工作管理模块的公益劳动基地控制层
 * @author Administrator
 */
@RestController
@RequestMapping("/labour")
public class LabourController {
	
	private static Logger logger = LogManager.getLogger(LabourController.class);
	
	@Autowired
	private ILabourService labourService;
	
	/**
	 * 新增公益劳动基地
	 * @param labour
	 * @return
	 */
	@RequestMapping("/add")
	public ResultData addLabour(@Valid Labour labour){ 
		logger.info("新增公益劳动基地"+labour);
		int i=labourService.addLabour(labour);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("增加成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("增加失败！");
		}
		return rd;
	}
	
	/**
	 * 删除公益劳动基地
	 * @param labour
	 * @return
	 */
	@RequestMapping("/del")
	public ResultData delLabour(Labour labour){ 
		logger.info("删除公益劳动基地"+labour);
		int i=labourService.delLabour(labour);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("删除成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("删除失败！");
		}
		return rd;
	}
	
	/**
	 * 修改公益劳动基地
	 * @param labour
	 * @return
	 */
	@RequestMapping("/update")
	public ResultData updateLabour(@Valid Labour labour){ 
		logger.info("修改公益劳动基地"+labour);
		int i=labourService.updateLabour(labour);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("修改成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("修改失败！");
		}
		return rd;
	}
	
	/**
	 * 根据id查询公益劳动基地
	 * @param labour
	 * @return
	 */
	@RequestMapping("/get")
	public ModelAndView getLabour(Labour labour){ 
		logger.info("查询公益劳动基地"+labour);
		labour=labourService.getLabour(labour);
		logger.info(labour);
		return new ModelAndView("work-manage/labour_edit","labour",labour);
	}
	
	
	/**
	 * 模糊查询公益劳动基地
	 * @param pageSize
	 * @param pageNum
	 * @param labour
	 * @return
	 */
	@RequestMapping("/querylike")
	public ResultData queryLikeLabours(int pageSize, int pageNum,Labour labour){ 
		logger.info("公益劳动基地查询条件"+labour);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageSize", pageSize);
		map.put("pageNum", pageNum);
		map.put("labour",labour);
		Page page=labourService.queryLikeLabours(map);
		List<Object> list=new ArrayList<Object>();
		list.add(page);
		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(list);
		return rd;
	}
	
	/**
	 * 导出公益劳动基地
	 * @param labour
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/export")
	public void exportLabours(Labour labour,HttpServletResponse response) throws IOException{ 
		logger.info("导出匹配的公益劳动基地"+labour);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("labour",labour);
		Workbook wb=labourService.exportLabours(map);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");  
        String excelName = "公益劳动基地";  
        response.setHeader("Content-Disposition","attachment; filename="+new String(excelName.getBytes("utf-8"), "ISO8859-1" )+".xls");  
		wb.write(response.getOutputStream());
	}
	
	/**
	 * 返回页面通用方法
	 * @param page
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(String page){
		return new ModelAndView(page);
	}
}